package com.qfedu.service.impl;

import com.qfedu.dao.FunctionDao;
import com.qfedu.dao.UserDao;
import com.qfedu.dao.UsergroupDao;
import com.qfedu.entity.Function;
import com.qfedu.entity.User;
import com.qfedu.entity.Usergroup;
import com.qfedu.service.UserService;
import com.qfedu.util.Constants;
import com.qfedu.util.Enctype;
import com.qfedu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * projectName: logistics
 * author: CGY
 * time: 2020/10/6 11:17
 * description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UsergroupDao usergroupDao;
    @Autowired
    private FunctionDao functionDao;

    @Override
    public Result login(String loginId, String password) {
        User user = userDao.findUserByLoginId(loginId);
        if(user == null){
            return Result.error(Constants.LOGINID_NOT_EXISTS);
        }else{
            String str = Enctype.MD5(password);
            if(!user.getPassword().equals(str)){
                return Result.error(Constants.PASSWORD_ERROR);
            }
        }
        return Result.success(user);
    }

    @Override
    public Result selectFunc(String loginId) {
        //有个问题 如果一个用户是多个角色的话 我们应该返回哪一个Id
        // 如果全部返回的话 需要把所有的权限全部查出 然后放在Map里去重
        Usergroup usergroup = usergroupDao.selectUsergroupByUserId(loginId);
        List<Function> functions = functionDao.selectFunctionByGroupId(usergroup.getId());
        return Result.success(functions);
    }
}
